package com.zcxy360.sandtable.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcxy360.sandtable.domain.Templet;
import com.zcxy360.sandtable.domain.TempletDrawing;
import com.zcxy360.sandtable.domain.TempletDrawingMapper;
import com.zcxy360.sandtable.domain.TempletExample;
import com.zcxy360.sandtable.domain.TempletExample.Criteria;
import com.zcxy360.sandtable.domain.TempletMapper;
import com.zcxy360.sandtable.domain.TempletPlan;
import com.zcxy360.sandtable.domain.TempletPlanMapper;
import com.zcxy360.sandtable.domain.TempletProblem;
import com.zcxy360.sandtable.domain.TempletProblemMapper;
import com.zcxy360.sandtable.domain.TempletVideo;
import com.zcxy360.sandtable.domain.TempletVideoMapper;
import com.zcxy360.sandtable.domain.User;
import com.zcxy360.sandtable.util.ShiroUtil;
import com.zcxy360.sandtable.util.StringUtil;

@Transactional
@Service
public class TempletService {
	
	@Autowired
	private TempletMapper templetMapper;
	@Autowired
	private TempletVideoMapper templetVideoMapper;
	@Autowired
	private TempletPlanMapper templetPlanMapper;
	@Autowired
	private TempletDrawingMapper templetDrawingMapper;
	@Autowired
	private TempletProblemMapper templetProblemMapper;

	public Templet getByTempletId(Integer templetId, Integer deep) {
		Templet templet = null;
		switch (deep) {
		case 0:
			templet = templetMapper.selectByPrimaryKey(templetId);
			break;
		case 1:
			templet = templetMapper.selectByPrimaryKeyWithExtra(templetId);
			break;
		case 2:
			templet = templetMapper.selectByPrimaryKeyWithCascade(templetId);
			break;
		case 3:
			templet = templetMapper.selectByPrimaryKeyWithExtraAndCascade(templetId);
			break;
		default:
			break;
		}
		return templet;
	}

	public Object list(Templet templet, String key, String orderByClause, Integer sign,Integer deep,Integer pageNum, Integer pageSize,Integer navigatePages) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			templet.setOrgId(loginUser.getOrgId());
		}
		if(sign != null) {
			//只看自己
			if(sign == 1 && !loginRole.equals("admin")) {
				templet.setUserId(loginUser.getUserId());
			}
			//看非自己的其他老师资源
			if(sign == 2) {
				templet.setUserId(loginUser.getUserId());
			}
			//看内建资源
			if(sign == 3) {
				templet.setUserId(1);
				templet.setOrgId(null);
			}
			//看本机构自己的所有资源及其它老师共享资源及内建资源
			if(sign == 4) {
				templet.setUserId(loginUser.getUserId());
			}
			//看本机构所有资源及内建资源
			if(sign == 5) {
			}
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Templet> templets = templetMapper.listWithExtra(templet,StringUtil.wrapper(key),orderByClause,sign);
		return new PageInfo<>(templets, navigatePages);
	}

	public Object post(Templet templet, int[] planIds, int[] videoIds, int[] drawingIds, int[] problemIds) {
		User loginUser = ShiroUtil.getUser();
		templet.setUserId(loginUser.getUserId());
		templet.setOrgId(loginUser.getOrgId());
		templet.setRoughTime(Math.ceil(templetMapper.countRoughTime(planIds,videoIds,drawingIds,problemIds)/30d)*0.5);
		templetMapper.insertSelective(templet);
		Integer templetId = templet.getTempletId();
		
		if(planIds != null && planIds.length > 0) {
			List<TempletPlan> templetPlans = new ArrayList<>();
			TempletPlan templetPlan;
			for(int planId:planIds) {
				templetPlan = new TempletPlan();
				templetPlan.setTempletId(templetId);
				templetPlan.setPlanId(planId);
				templetPlans.add(templetPlan);
			}
			templetPlanMapper.insertBatch(templetPlans);
		}
		if(videoIds != null && videoIds.length > 0) {
			List<TempletVideo> templetVideos = new ArrayList<>();
			TempletVideo templetVideo;
			for(int videoId:videoIds) {
				templetVideo = new TempletVideo();
				templetVideo.setTempletId(templetId);
				templetVideo.setVideoId(videoId);
				templetVideos.add(templetVideo);
			}
			templetVideoMapper.insertBatch(templetVideos);
		}
		if(drawingIds != null && drawingIds.length > 0) {
			List<TempletDrawing> templetDrawings = new ArrayList<>();
			TempletDrawing templetDrawing;
			for(int drawingId:drawingIds) {
				templetDrawing = new TempletDrawing();
				templetDrawing.setTempletId(templetId);
				templetDrawing.setDrawingId(drawingId);
				templetDrawings.add(templetDrawing);
			}
			templetDrawingMapper.insertBatch(templetDrawings);
		}
		if(problemIds != null && problemIds.length > 0) {
			List<TempletProblem> templetProblems = new ArrayList<>();
			TempletProblem templetProblem;
			for(int problemId:problemIds) {
				templetProblem = new TempletProblem();
				templetProblem.setTempletId(templetId);
				templetProblem.setProblemId(problemId);
				templetProblems.add(templetProblem);
			}
			templetProblemMapper.insertBatch(templetProblems);
		}
		return templet;
	}

	public boolean update(Templet templet, int[] planIds, int[] videoIds, int[] drawingIds, int[] problemIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(!loginRole.equals("admin")) {
			Templet t = templetMapper.selectByPrimaryKey(templet.getTempletId());
			if(loginRole.equals("teacher")) {
				if(!t.getUserId().equals(loginUser.getUserId())) {
					throw new RuntimeException("老师只能修改本人的资源");
				}
			}
			if(loginRole.equals("manager")) {
				if(!t.getOrgId().equals(loginUser.getOrgId())) {
					throw new RuntimeException("机构管理员只能修改本机构的资源");
				}
			}
		}
		Integer templetId = templet.getTempletId();
		if(planIds != null || videoIds != null || drawingIds != null || problemIds != null) {
			templetMapper.deleteReferenceByTempletId(templetId);
			templet.setRoughTime(Math.ceil(templetMapper.countRoughTime(planIds,videoIds,drawingIds,problemIds)/30d)*0.5);
			if(planIds != null && planIds.length > 0) {
				List<TempletPlan> templetPlans = new ArrayList<>();
				TempletPlan templetPlan;
				for(int planId:planIds) {
					templetPlan = new TempletPlan();
					templetPlan.setTempletId(templetId);
					templetPlan.setPlanId(planId);
					templetPlans.add(templetPlan);
				}
				templetPlanMapper.insertBatch(templetPlans);
			}
			if(videoIds != null && videoIds.length > 0) {
				List<TempletVideo> templetVideos = new ArrayList<>();
				TempletVideo templetVideo;
				for(int videoId:videoIds) {
					templetVideo = new TempletVideo();
					templetVideo.setTempletId(templetId);
					templetVideo.setVideoId(videoId);
					templetVideos.add(templetVideo);
				}
				templetVideoMapper.insertBatch(templetVideos);
			}
			if(drawingIds != null && drawingIds.length > 0) {
				List<TempletDrawing> templetDrawings = new ArrayList<>();
				TempletDrawing templetDrawing;
				for(int drawingId:drawingIds) {
					templetDrawing = new TempletDrawing();
					templetDrawing.setTempletId(templetId);
					templetDrawing.setDrawingId(drawingId);
					templetDrawings.add(templetDrawing);
				}
				templetDrawingMapper.insertBatch(templetDrawings);
			}
			if(problemIds != null && problemIds.length > 0) {
				List<TempletProblem> templetProblems = new ArrayList<>();
				TempletProblem templetProblem;
				for(int problemId:problemIds) {
					templetProblem = new TempletProblem();
					templetProblem.setTempletId(templetId);
					templetProblem.setProblemId(problemId);
					templetProblems.add(templetProblem);
				}
				templetProblemMapper.insertBatch(templetProblems);
			}
		}
		templetMapper.updateByPrimaryKeySelective(templet);
		if(templet.getIsShared() != null && templet.getIsShared() == 1) {
			//使引用资源共享
			templetMapper.updateReferShared(templetId);
		}
		return true;
	}

	public boolean delete(int[] templetIds) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		Templet t;
		if(!loginRole.equals("admin")) {
			for(int templetId:templetIds) {
				t = templetMapper.selectByPrimaryKey(templetId);
				if(loginRole.equals("teacher")) {
					if(!t.getUserId().equals(loginUser.getUserId())) {
						throw new RuntimeException("老师只能删除本人的资源");
					}
				}
				if(loginRole.equals("manager")) {
					if(!t.getOrgId().equals(loginUser.getOrgId())) {
						throw new RuntimeException("机构管理员只能删除本机构的资源");
					}
				}
			}
		}
		templetMapper.deleteBatch(templetIds);
		return true;
	}

	public boolean checkName(String name) {
		User loginUser = ShiroUtil.getUser();
		TempletExample ex = new TempletExample();
		Criteria or = ex.or();
		or.andNameEqualTo(name);
		if(loginUser.getRole().equals("admin")) {
			or.andOrgIdIsNull();
		}else {
			or.andOrgIdEqualTo(loginUser.getOrgId());
		}
		return templetMapper.countByExample(ex) == 0;
	}

	
}
